package com.da.lon.wang.xlg.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.StoreListResult.StoreInfo;

import java.util.ArrayList;

public class StoreListAdapter extends BaseAdapter {
    ArrayList<StoreInfo> datas;
    Context context;
    LayoutInflater inflater;
    double[] coordinates;

    public StoreListAdapter(ArrayList<StoreInfo> datas, double[] coordinates,
                            Context context) {
        this.datas = datas;
        this.context = context;
        this.coordinates = coordinates;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return datas != null ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas != null && datas.size() > position ? datas.get(position)
                : null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            Holder holder = new Holder();
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.store_list_item, null);
            }
            holder.img = (ImageView) convertView
                    .findViewById(R.id.store_bigimage);
            holder.name = (TextView) convertView.findViewById(R.id.store_name);
            holder.dis = (TextView) convertView
                    .findViewById(R.id.store_distance);
            if (datas != null && position < datas.size()
                    && datas.get(position) != null
                    && datas.get(position).getStore_name() != null
                    && datas.get(position).getDistance() != null) {
                holder.name.setText(datas.get(position).getStore_name());
                String distance = datas.get(position).getDistance() != null ? datas.get(position).getDistance() : "0";
                int dis = Integer.parseInt(distance);
                if (coordinates[0] == 0 && coordinates[1] == 0) {
                    holder.dis.setText("定位失败");
                } else if (dis < 1000) {
                    holder.dis.setText("距离" + dis + "米");
                } else {
                    holder.dis.setText("距离" + dis / 1000 + "千米");
                }
                if (datas.get(position).getImage_1() != null
                        && !"".equals(datas.get(position).getImage_1()))
//					MyApplication.loadImage(context, datas.get(position)
//							.getImage_1(), holder.img);
                    MyApplication.loadImageByPicasso(context, datas.get(position)
                            .getImage_1(), null, holder.img, 0, 0, 0);
            }
            if (convertView.getTag() == null) {
                convertView.setTag(holder);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    class Holder {
        ImageView img;
        TextView name;
        TextView dis;
    }

}
